/*
 * $Id: PolisAclGroupTestCase.java 27 2007-04-13 01:02:48Z aaron.day $
 */
package org.crazydays.core.data;


import org.hibernate.Session;
import org.hibernate.HibernateException;

import junit.framework.TestCase;

import org.crazydays.hibernate.HibernateUtil;

/**
 * PolisUserTestCase
 */
public class PolisAclGroupTestCase
	extends TestCase
{
	/**
	 * Test for allow.
	 * 
	 * @param aclName Acl name
	 * @param groupName Group name
	 * @param rightName Right name
	 * @throws HibernateException
	 */
	public void testAllow(String aclName, String groupName, String rightName)
		throws HibernateException
	{
		if (aclName == null) {
			throw new IllegalArgumentException("aclName == null");
		}
		if (groupName == null) {
			throw new IllegalArgumentException("groupName == null");
		}
		if (rightName == null) {
			throw new IllegalArgumentException("rightName == null");
		}

		Session session = HibernateUtil.getSession();
		try {
			PolisAcl acl = (PolisAcl) HibernateUtil
					.loadPolisDataByName(session,
							PolisAcl.class, aclName);
			assertNotNull("acl == null", acl);

			PolisGroup group = (PolisGroup) HibernateUtil
					.loadPolisDataByName(session,
							PolisGroup.class,
							groupName);
			assertNotNull("group == null", group);

			PolisRight right = (PolisRight) HibernateUtil
					.loadPolisDataByName(session,
							PolisRight.class,
							rightName);
			assertNotNull("right == null", right);

			PolisAclGroupKey key = new PolisAclGroupKey();
			key.setAclId(acl.getId());
			key.setGroupId(group.getId());
			key.setRightId(right.getId());

			PolisAclGroup aclGroup = (PolisAclGroup) HibernateUtil
					.loadObject(session,
							PolisAclGroup.class,
							key);

			assertNotNull(aclGroup);
			assertTrue("aclGroup.allow != true", aclGroup
					.getAllow());

		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	/**
	 * Test Guest acl.
	 */
	public void testGuestAcl()
	{
		testAllow("Guest", "Guest", "Execute");
		testAllow("Guest", "Guest", "Read");
		testAllow("Guest", "Admin", "Admin");
		testAllow("Guest", "Admin", "Execute");
		testAllow("Guest", "Admin", "Read");
		testAllow("Guest", "Admin", "Write");
		testAllow("Guest", "User", "Execute");
		testAllow("Guest", "User", "Read");
	}

	/**
	 * Test Admin acl.
	 */
	public void testAdminAcl()
	{
		testAllow("Admin", "Admin", "Admin");
		testAllow("Admin", "Admin", "Execute");
		testAllow("Admin", "Admin", "Read");
		testAllow("Admin", "Admin", "Write");
	}

	/**
	 * Test User acl.
	 */
	public void testUserAcl()
	{
		testAllow("User", "Admin", "Admin");
		testAllow("User", "Admin", "Execute");
		testAllow("User", "Admin", "Read");
		testAllow("User", "Admin", "Write");
		testAllow("User", "User", "Execute");
		testAllow("User", "User", "Read");
		testAllow("User", "User", "Write");
	}
}
